//
//  SetViewController.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "SetViewController.h"

#define Width self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface SetViewController ()
@property (nonatomic, assign) CGFloat memoryNum;
@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
        //初始化内存大小
    self.memoryNum = 0;
    [self numMemory];//计算缓存
    [self creatView];//创建View

}
- (void)numMemory
{
    //创建管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //获取路径
    NSString *catchPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    if ([fileManager fileExistsAtPath:catchPath]) {
        NSArray *files = [fileManager subpathsAtPath:catchPath];
        //遍历所有文件
        for (NSInteger i = 0; i < files.count; i++) {
            NSString *filePath = [catchPath stringByAppendingPathComponent:files[i]];
            //获取文件大小
            float k = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
            self.memoryNum += k;
        }
        
    }

}
- (void)creatView
{
    for (NSInteger i= 0; i < 4; i++) {
        UIView *setView = [[UIView alloc] initWithFrame:CGRectMake(0, i *80 + 5, Width, 80)];
        UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 40)];
        [setView addSubview:dayLabel];
        [dayLabel release];
        if (i == 0) {
            dayLabel.text = @"夜间模式";
            UISwitch *swc = [[UISwitch alloc] initWithFrame:CGRectMake(Width - 20 - 60, 20, 60, 40)];
            swc.tag = 6000;
            [swc addTarget:self action:@selector(swcAction:) forControlEvents:UIControlEventValueChanged];
            [setView addSubview:swc];
            [swc release];
        }else if (i == 1){
            dayLabel.text = @"清除缓存";
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [setView addGestureRecognizer:tap];
            [tap release];
            UILabel *huancunLabel = [[UILabel alloc] initWithFrame:CGRectMake(Width - 20 - 60, 20, 100, 40)];
            huancunLabel.tag = 1000;
#pragma mark - 显示缓存大小
            huancunLabel.text = [NSString stringWithFormat:@"%.2fM", self.memoryNum / 1024 / 1024];
            [setView addSubview:huancunLabel];
            [huancunLabel release];
        }else if (i == 2){
            dayLabel.text = @"登陆片刻";
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapIsLogin:)];
            [setView addGestureRecognizer:tap];
            [tap release];
        }else{
            dayLabel.text = @"退出应用";
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBreak:)];
            [setView addGestureRecognizer:tap];
            [tap release];
        }
        [self.view addSubview:setView];
        [setView release];
    }
}
#pragma mark - 手势触发方法 清除缓存
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSFileManager *fManager = [NSFileManager defaultManager];
    NSString *catchPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    [fManager removeItemAtPath:catchPath error:nil];//删除缓存
    UILabel *label = (UILabel *)[self.view viewWithTag:1000];
    label.text = @"0.00M";
    [self promptViewWithMessage:@"清除成功"];
    
}
- (void)promptViewWithMessage:(NSString *)message
{
    UIView *tishiView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    tishiView.center = self.view.center;
    tishiView.backgroundColor = [UIColor blackColor];
    tishiView.alpha = 0.5;
    tishiView.layer.cornerRadius = 10;
    [self.view addSubview:tishiView];
    [tishiView release];
    UILabel *tishilabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 70, 30)];
    tishilabel.textColor = [UIColor whiteColor];
    tishilabel.text = message;
    [tishiView addSubview:tishilabel];
    [tishilabel release];
    //延时
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [tishiView removeFromSuperview];
    });

}
- (void)tapIsLogin:(UITapGestureRecognizer *)tap
{
    NSLog(@"登陆");
}
- (void)tapBreak:(UITapGestureRecognizer *)tap
{
    NSLog(@"退出");
}
#pragma mark - 视图将要出现时判断开关状态
- (void)viewWillAppear:(BOOL)animated
{
    UISwitch *swc = [self.view viewWithTag:6000];
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"];
    if ([str isEqualToString:@"Night"]) {
        swc.on = YES;
    }
}

#pragma mark - 夜间模式开关
- (void)swcAction:(UISwitch *)swc
{
    if (swc.isOn) {
        [[NSUserDefaults standardUserDefaults] setObject:@"Night" forKey:@"Mode"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"模式" object:@"Night" userInfo:nil];
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"模式" object:@"Day" userInfo:nil];
         [[NSUserDefaults standardUserDefaults] setObject:@"Day" forKey:@"Mode"];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
